package com.gome.boot.config.datasource.multiple;

import com.gome.boot.config.datasource.BaseDataSourceConfig;
import com.gome.boot.config.datasource.IDataSourceTransactionConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.util.Map;

/**
 * 多数据源配置.
 *
 * @author baoxiufeng
 */
public abstract class MultipleDataSourceConfig extends BaseDataSourceConfig implements IDataSourceTransactionConfig {

    @Bean
    public MultipleDataSource multipleDataSource() throws Exception {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        // 设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(getDefaultTargetDataSource());
        // 设置所有数据源集合
        multipleDataSource.setTargetDataSources(getAllDataSource());
        return multipleDataSource;
    }

    /**
     * 获取所有的数据源集合.
     * <br/>应用需要自己去实现.
     *
     * @return 所有的数据源集合
     * @throws Exception 异常
     */
    protected abstract Map<Object, Object> getAllDataSource() throws Exception;

    /**
     * 获取默认的数据源实例.
     *
     * @return 默认的数据源实例
     * @throws Exception 异常
     */
    protected abstract Object getDefaultTargetDataSource() throws Exception;

    @Override
    public DataSourceTransactionManager transactionManager() throws Exception {
        return null;
    }
}
